﻿/*
' DotNetNuke® - http://www.dotnetnuke.com
' Copyright (c) 2002-2009
' by DotNetNuke Corporation
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
*/

using WatiN.Core;
using DotNetNuke.Tests.UI.WatiN.Common;

namespace DotNetNuke.Tests.UI.WatiN.Common.WatiNObjects
{
    public class ProfilePropertiesPage : WatiNBase
    {
        #region Constructors

        public ProfilePropertiesPage(WatiNBase watinBase) : base(watinBase.IEInstance, watinBase.SiteUrl, watinBase.DBName) { }

        public ProfilePropertiesPage(IE ieInstance, string siteUrl, string dbName) : base(ieInstance, siteUrl, dbName) { }

        #endregion

        #region Public Properties

        #region Links
        public Link AddNewProfilePropertyLink
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.Link(Find.ByTitle("Add New Profile Property"));
                }
                return IEInstance.Link(Find.ByTitle("Add New Profile Property"));
            }
        }
        public Link NextButton
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.Link(Find.ById(s => s.EndsWith("EditProfileDefinition_Wizard_StartNavigationTemplateContainerID_StartNextLinkButton")));
                }
                return IEInstance.Link(Find.ById(s => s.EndsWith("EditProfileDefinition_Wizard_StartNavigationTemplateContainerID_StartNextLinkButton")));
            }
        }
        public Link ReturnToProfilePropertiesLink
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.Link(Find.ById(s => s.EndsWith("EditProfileDefinition_Wizard_FinishNavigationTemplateContainerID_FinishLinkButton")));
                }
                return IEInstance.Link(Find.ById(s => s.EndsWith("EditProfileDefinition_Wizard_FinishNavigationTemplateContainerID_FinishLinkButton")));
            }
        }
        #endregion

        #region TextFields
        public TextField PropertyNameField
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.TextField(Find.ByName(s => s.EndsWith("$PropertyName")));
                }
                return IEInstance.TextField(Find.ByName(s => s.EndsWith("$PropertyName")));
            }
        }
        public TextField PropertyCategoryField
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.TextField(Find.ByName(s => s.EndsWith("$PropertyCategory")));
                }
                return IEInstance.TextField(Find.ByName(s => s.EndsWith("$PropertyCategory")));
            }
        }
        public TextField LengthTextField
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.TextField(Find.ByName(s => s.EndsWith("$Length")));
                }
                return IEInstance.TextField(Find.ByName(s => s.EndsWith("$Length")));
            }
        }
        #endregion

        #region SelectLists
        public SelectList DataTypeSelectList
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.SelectList(Find.ByName(s => s.EndsWith("$DataType")));
                }
                return IEInstance.SelectList(Find.ByName(s => s.EndsWith("$DataType")));
            }
        }
        #endregion

        #region CheckBoxes
        public CheckBox VisibleCheckBox
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.CheckBox(Find.ByName(s => s.EndsWith("$Visible")));
                }
                return IEInstance.CheckBox(Find.ByName(s => s.EndsWith("$Visible")));
            }
        }
        public CheckBox RequiredCheckBox
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.CheckBox(Find.ByName(s => s.EndsWith("$Required")));
                }
                return IEInstance.CheckBox(Find.ByName(s => s.EndsWith("$Required")));
            }
        }
        #endregion

        #region Tables
        public Table PropertyTable
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.Table(Find.ById(s => s.EndsWith("ProfileDefinitions_grdProfileProperties")));
                }
                return IEInstance.Table(Find.ById(s => s.EndsWith("ProfileDefinitions_grdProfileProperties")));
            }
        }
        public Table PropertyDefTable
        {
            get { return ContentDiv.Table(Find.ById(s => s.EndsWith("EditProfileDefinition_Wizard_Properties_tbl"))); }
        }
        #endregion

        #endregion

        #region Public Methods

        public TableRow GetPropertyRow(string propertyName)
        {
            //Returns the property row with the specified property name
            TableRow propertyRow = null;
            foreach (TableRow row in PropertyTable.TableRows)
            {
                if (row.TableCells[4].InnerHtml.Contains(propertyName))
                {
                    propertyRow = row;
                    break;
                }
                continue;
            }
            return propertyRow;
        }

        public Image GetPropertyEditButton(string propertyName)
        {
            //Returns the property row with the specified property name
            Image result = null;
            foreach (TableRow row in PropertyTable.TableRows)
            {
                if (row.TableCells[4].InnerHtml.Contains(propertyName))
                {
                    result = row.Image(Find.ByTitle("Edit"));
                    break;
                }
                continue;
            }
            return result;
        }

        public TableCell GetPropertyNameCell(TableRow propertyRow)
        {
            return propertyRow.TableCells[4];
        }

        public TableCell GetPropertyCategoryCell(TableRow propertyRow)
        {
            return propertyRow.TableCells[5];
        }

        public TableCell GetPropertyDataTypeCell(TableRow propertyRow)
        {
            return propertyRow.TableCells[6];
        }

        public TableCell GetPropertyVisibilityCell(TableRow propertyRow)
        {
            if (IEInstance.Url.Contains("Superuser%20Accounts"))
            {
                return propertyRow.TableCells[11];
            }
            return propertyRow.TableCells[12];
        }

        

        #endregion
    }
}